package com.example.system.information.utils;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.example.system.information.CrashApplication;
import com.hjq.permissions.XXPermissions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author : King
 * date   : 2023/11/1018:22
 * desc   : 通用工具
 */
public class CommonUtils {

    public static String getImeiID(){
        String imei = "";

        if (XXPermissions.isHasPermission(CrashApplication.getContext() , Manifest.permission.READ_PHONE_STATE)
                && XXPermissions.isHasPermission(CrashApplication.getContext() , Manifest.permission.ACCESS_FINE_LOCATION)
                && XXPermissions.isHasPermission(CrashApplication.getContext() , Manifest.permission.ACCESS_COARSE_LOCATION)) {
            TelephonyManager mPhone = (TelephonyManager) CrashApplication.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            if (mPhone.getDeviceId() != null) {
                imei = mPhone.getDeviceId();
            } else {
                imei = Settings.Secure.getString(CrashApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }

        return imei ;
    }

    /**
     * 判断网络是否可用
     */
    public static boolean isConnectIsNormal() {
        ConnectivityManager connectivityManager = (ConnectivityManager) CrashApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取版本号
     *
     * @return
     */
    public static int getVersionCode() {
        PackageManager manager = CrashApplication.getContext().getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(CrashApplication.getContext().getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取版本号
     * @return
     */
    public static String getVersionName() {
        PackageManager manager = CrashApplication.getContext().getPackageManager();
        String code = "";
        try {
            PackageInfo info = manager.getPackageInfo(CrashApplication.getContext().getPackageName(), 0);
            code = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取Sim卡信息
     * @return
     */
    public static String getSimCode(){
        if (XXPermissions.isHasPermission(CrashApplication.getContext() , Manifest.permission.READ_PHONE_STATE)){
            TelephonyManager telephonyManager = (TelephonyManager) CrashApplication.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            return telephonyManager.getSubscriberId();
        }else {
            return "";
        }
    }

    /**
     * 获取非null数据
     * @param text
     * @return
     */
    public static String getNormalString(String text){
        if(TextUtils.isEmpty(text))
            return "";

        return text ;
    }

    public static String getDateAdd(String day, int Num) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = df.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果需要向后计算日期 -改为+
        Date newDate2 = new Date(nowDate.getTime() + (long) Num * 60 * 60 * 1000);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateOk = simpleDateFormat.format(newDate2);
        return dateOk;
    }

    /**
     * 判断服务是否在运行
     * @param componentName
     * @return
     */
    public static boolean serverIsRunning(String componentName) {
        ActivityManager activityManager = (ActivityManager) CrashApplication.getContext().getSystemService(CrashApplication.getContext().ACTIVITY_SERVICE);

        List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (runningServices.size() <= 0) {
            return false;
        }

        for (ActivityManager.RunningServiceInfo serviceInfo : runningServices) {
            if (componentName.equals(serviceInfo.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 启动服务
     * @param className
     */
    public static void startService(@NonNull final String className) {
        try {
            Intent intent = new Intent(CrashApplication.getContext(), Class.forName(className));
            intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CrashApplication.getContext().startForegroundService(intent);
            } else {
                CrashApplication.getContext().startService(intent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取时间字符串
     * @return
     */
    public static String getTimeStr(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    /**
     * 关闭服务
     * @param className
     */
    public static void stopService(@NonNull final String className) {
        try {
            Intent intent = new Intent(CrashApplication.getContext(), Class.forName(className));
            intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            CrashApplication.getContext().stopService(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 清空应用缓存
     */
    public static void clearData(){
        ActivityManager manager = (ActivityManager) CrashApplication.getContext().getSystemService(Context.ACTIVITY_SERVICE);
        manager.clearApplicationUserData();
    }
}
